home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / zoneinfo / src / zic.c < prev   
C/C++ Source or Header  |  1994-08-26  |  46KB  |  1,933 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Arthur David Olson of the National Cancer Institute.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)zic.c    5.3 (Berkeley) 4/20/91";
  39. #endif /* not lint */
  40.  
  41. #ifdef notdef
  42. static char    elsieid[] = "@(#)zic.c    4.12";
  43. #endif
  44.  
  45. #include <sys/types.h>
  46. #include <sys/cdefs.h>
  47. #include <sys/stat.h>
  48. #include <time.h>
  49. #include <tzfile.h>
  50. #include <stdio.h>
  51. #include <ctype.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54.  
  55. FILE *
  56. dj_fopen(const char *fn, char *mode)
  57. {
  58.   char *cp;
  59.   FILE *f;
  60.   char *fn2 = (char *)malloc(strlen(fn)+1);
  61.   strcpy(fn2, fn);
  62.   for (cp=fn2; *cp; cp++)
  63.     if (*cp == '+')
  64.       *cp = '%';
  65.   f = fopen(fn2, mode);
  66.   free(fn2);
  67.   return f;
  68. }
  69. #define fopen dj_fopen
  70.  
  71. int dj_link(char *from, char *to)
  72. {
  73.   FILE *f, *t;
  74.   char buf[4096];
  75.   int b;
  76.   f = fopen(from, "rb");
  77.   if (f == 0)
  78.     return -1;
  79.   t = fopen(to, "wb");
  80.   if (t == 0)
  81.   {
  82.     fclose(f);
  83.     return -1;
  84.   }
  85.   while ((b=fread(buf, 1, 4096, f))>0)
  86.     fwrite(buf, 1, b, t);
  87.   fclose(f);
  88.   fclose(t);
  89.   return 0;
  90. }
  91. #define link dj_link
  92.  
  93. #ifndef TRUE
  94. #define TRUE    1
  95. #define FALSE    0
  96. #endif /* !defined TRUE */
  97.  
  98. struct rule {
  99.     const char *    r_filename;
  100.     int        r_linenum;
  101.     const char *    r_name;
  102.  
  103.     int        r_loyear;    /* for example, 1986 */
  104.     int        r_hiyear;    /* for example, 1986 */
  105.     const char *    r_yrtype;
  106.  
  107.     int        r_month;    /* 0..11 */
  108.  
  109.     int        r_dycode;    /* see below */
  110.     int        r_dayofmonth;
  111.     int        r_wday;
  112.  
  113.     long        r_tod;        /* time from midnight */
  114.     int        r_todisstd;    /* above is standard time if TRUE */
  115.                     /* or wall clock time if FALSE */
  116.     long        r_stdoff;    /* offset from standard time */
  117.     const char *    r_abbrvar;    /* variable part of abbreviation */
  118.  
  119.     int        r_todo;        /* a rule to do (used in outzone) */
  120.     time_t        r_temp;        /* used in outzone */
  121. };
  122.  
  123. /*
  124. **    r_dycode        r_dayofmonth    r_wday
  125. */
  126.  
  127. #define DC_DOM        0    /* 1..31 */    /* unused */
  128. #define DC_DOWGEQ    1    /* 1..31 */    /* 0..6 (Sun..Sat) */
  129. #define DC_DOWLEQ    2    /* 1..31 */    /* 0..6 (Sun..Sat) */
  130.  
  131. struct zone {
  132.     const char *    z_filename;
  133.     int        z_linenum;
  134.  
  135.     const char *    z_name;
  136.     long        z_gmtoff;
  137.     const char *    z_rule;
  138.     const char *    z_format;
  139.  
  140.     long        z_stdoff;
  141.  
  142.     struct rule *    z_rules;
  143.     int        z_nrules;
  144.  
  145.     struct rule    z_untilrule;
  146.     time_t        z_untiltime;
  147. };
  148.  
  149. extern char *    icatalloc __P((char * old, const char * new));
  150. extern char *    icpyalloc __P((const char * string));
  151. extern void    ifree __P((char * p));
  152. extern char *    imalloc __P((int n));
  153. extern char *    irealloc __P((char * old, int n));
  154. /* extern int    link __P((const char * fromname, const char * toname));*/
  155. extern char *    optarg;
  156. extern int    optind;
  157. extern void    perror __P((const char * string));
  158. extern char *    scheck __P((const char * string, const char * format));
  159. static void    addtt __P((time_t starttime, int type));
  160. static int    addtype
  161.             __P((long gmtoff, const char * abbr, int isdst,
  162.             int ttisstd));
  163. static void    addleap __P((time_t t, int positive, int rolling));
  164. static void    adjleap __P((void));
  165. static void    associate __P((void));
  166. static int    ciequal __P((const char * ap, const char * bp));
  167. static void    convert __P((long val, char * buf));
  168. static void    dolink __P((const char * fromfile, const char * tofile));
  169. static void    eat __P((const char * name, int num));
  170. static void    eats __P((const char * name, int num,
  171.             const char * rname, int rnum));
  172. static long    eitol __P((int i));
  173. static void    error __P((const char * message));
  174. static char **    getfields __P((char * buf));
  175. static long    gethms __P((char * string, const char * errstrng,
  176.             int signable));
  177. static void    infile __P((const char * filename));
  178. static void    inleap __P((char ** fields, int nfields));
  179. static void    inlink __P((char ** fields, int nfields));
  180. static void    inrule __P((char ** fields, int nfields));
  181. static int    inzcont __P((char ** fields, int nfields));
  182. static int    inzone __P((char ** fields, int nfields));
  183. static int    inzsub __P((char ** fields, int nfields, int iscont));
  184. static int    itsabbr __P((const char * abbr, const char * word));
  185. static int    itsdir __P((const char * name));
  186. static int    lowerit __P((int c));
  187. static char *    memcheck __P((char * tocheck));
  188. static int    mkdirs __P((char * filename));
  189. static void    newabbr __P((const char * abbr));
  190. static long    oadd __P((long t1, long t2));
  191. static void    outzone __P((const struct zone * zp, int ntzones));
  192. static void    puttzcode __P((long code, FILE * fp));
  193. static int    rcomp __P((const void *leftp, const void *rightp));
  194. static time_t    rpytime __P((const struct rule * rp, int wantedy));
  195. static void    rulesub __P((struct rule * rp, char * loyearp, char * hiyearp,
  196.         char * typep, char * monthp, char * dayp, char * timep));
  197. static void    setboundaries __P((void));
  198. static time_t    tadd __P((time_t t1, long t2));
  199. static void    usage __P((void));
  200. static void    writezone __P((const char * name));
  201. static int    yearistype __P((int year, const char * type));
  202.  
  203. static int        charcnt;
  204. static int        errors;
  205. static const char *    filename;
  206. static int        leapcnt;
  207. static int        linenum;
  208. static time_t        max_time;
  209. static int        max_year;
  210. static time_t        min_time;
  211. static int        min_year;
  212. static int        noise;
  213. static const char *    rfilename;
  214. static int        rlinenum;
  215. static const char *    progname;
  216. static int        timecnt;
  217. static int        typecnt;
  218. static int        tt_signed;
  219.  
  220. /*
  221. ** Line codes.
  222. */
  223.  
  224. #define LC_RULE        0
  225. #define LC_ZONE        1
  226. #define LC_LINK        2
  227. #define LC_LEAP        3
  228.  
  229. /*
  230. ** Which fields are which on a Zone line.
  231. */
  232.  
  233. #define ZF_NAME        1
  234. #define ZF_GMTOFF    2
  235. #define ZF_RULE        3
  236. #define ZF_FORMAT    4
  237. #define ZF_TILYEAR    5
  238. #define ZF_TILMONTH    6
  239. #define ZF_TILDAY    7
  240. #define ZF_TILTIME    8
  241. #define ZONE_MINFIELDS    5
  242. #define ZONE_MAXFIELDS    9
  243.  
  244. /*
  245. ** Which fields are which on a Zone continuation line.
  246. */
  247.  
  248. #define ZFC_GMTOFF    0
  249. #define ZFC_RULE    1
  250. #define ZFC_FORMAT    2
  251. #define ZFC_TILYEAR    3
  252. #define ZFC_TILMONTH    4
  253. #define ZFC_TILDAY    5
  254. #define ZFC_TILTIME    6
  255. #define ZONEC_MINFIELDS    3
  256. #define ZONEC_MAXFIELDS    7
  257.  
  258. /*
  259. ** Which files are which on a Rule line.
  260. */
  261.  
  262. #define RF_NAME        1
  263. #define RF_LOYEAR    2
  264. #define RF_HIYEAR    3
  265. #define RF_COMMAND    4
  266. #define RF_MONTH    5
  267. #define RF_DAY        6
  268. #define RF_TOD        7
  269. #define RF_STDOFF    8
  270. #define RF_ABBRVAR    9
  271. #define RULE_FIELDS    10
  272.  
  273. /*
  274. ** Which fields are which on a Link line.
  275. */
  276.  
  277. #define LF_FROM        1
  278. #define LF_TO        2
  279. #define LINK_FIELDS    3
  280.  
  281. /*
  282. ** Which fields are which on a Leap line.
  283. */
  284.  
  285. #define LP_YEAR        1
  286. #define LP_MONTH    2
  287. #define LP_DAY        3
  288. #define LP_TIME        4
  289. #define LP_CORR        5
  290. #define LP_ROLL        6
  291. #define LEAP_FIELDS    7
  292.  
  293. /*
  294. ** Year synonyms.
  295. */
  296.  
  297. #define YR_MINIMUM    0
  298. #define YR_MAXIMUM    1
  299. #define YR_ONLY        2
  300.  
  301. static struct rule *    rules;
  302. static int        nrules;    /* number of rules */
  303.  
  304. static struct zone *    zones;
  305. static int        nzones;    /* number of zones */
  306.  
  307. struct link {
  308.     const char *    l_filename;
  309.     int        l_linenum;
  310.     const char *    l_from;
  311.     const char *    l_to;
  312. };
  313.  
  314. static struct link *    links;
  315. static int        nlinks;
  316.  
  317. struct lookup {
  318.     const char *    l_word;
  319.     const int    l_value;
  320. };
  321.  
  322. static struct lookup const *    byword __P((const char * string,
  323.                     const struct lookup * lp));
  324.  
  325. static struct lookup const    line_codes[] = {
  326.     "Rule",        LC_RULE,
  327.     "Zone",        LC_ZONE,
  328.     "Link",        LC_LINK,
  329.     "Leap",        LC_LEAP,
  330.     NULL,        0
  331. };
  332.  
  333. static struct lookup const    mon_names[] = {
  334.     "January",    TM_JANUARY,
  335.     "February",    TM_FEBRUARY,
  336.     "March",    TM_MARCH,
  337.     "April",    TM_APRIL,
  338.     "May",        TM_MAY,
  339.     "June",        TM_JUNE,
  340.     "July",        TM_JULY,
  341.     "August",    TM_AUGUST,
  342.     "September",    TM_SEPTEMBER,
  343.     "October",    TM_OCTOBER,
  344.     "November",    TM_NOVEMBER,
  345.     "December",    TM_DECEMBER,
  346.     NULL,        0
  347. };
  348.  
  349. static struct lookup const    wday_names[] = {
  350.     "Sunday",    TM_SUNDAY,
  351.     "Monday",    TM_MONDAY,
  352.     "Tuesday",    TM_TUESDAY,
  353.     "Wednesday",    TM_WEDNESDAY,
  354.     "Thursday",    TM_THURSDAY,
  355.     "Friday",    TM_FRIDAY,
  356.     "Saturday",    TM_SATURDAY,
  357.     NULL,        0
  358. };
  359.  
  360. static struct lookup const    lasts[] = {
  361.     "last-Sunday",        TM_SUNDAY,
  362.     "last-Monday",        TM_MONDAY,
  363.     "last-Tuesday",        TM_TUESDAY,
  364.     "last-Wednesday",    TM_WEDNESDAY,
  365.     "last-Thursday",    TM_THURSDAY,
  366.     "last-Friday",        TM_FRIDAY,
  367.     "last-Saturday",    TM_SATURDAY,
  368.     NULL,            0
  369. };
  370.  
  371. static struct lookup const    begin_years[] = {
  372.     "minimum",        YR_MINIMUM,
  373.     "maximum",        YR_MAXIMUM,
  374.     NULL,            0
  375. };
  376.  
  377. static struct lookup const    end_years[] = {
  378.     "minimum",        YR_MINIMUM,
  379.     "maximum",        YR_MAXIMUM,
  380.     "only",            YR_ONLY,
  381.     NULL,            0
  382. };
  383.  
  384. static struct lookup const    leap_types[] = {
  385.     "Rolling",        TRUE,
  386.     "Stationary",        FALSE,
  387.     NULL,            0
  388. };
  389.  
  390. static const int    len_months[2][MONSPERYEAR] = {
  391.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  392.     31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  393. };
  394.  
  395. static const int    len_years[2] = {
  396.     DAYSPERNYEAR, DAYSPERLYEAR
  397. };
  398.  
  399. static time_t        ats[TZ_MAX_TIMES];
  400. static unsigned char    types[TZ_MAX_TIMES];
  401. static long        gmtoffs[TZ_MAX_TYPES];
  402. static char        isdsts[TZ_MAX_TYPES];
  403. static char        abbrinds[TZ_MAX_TYPES];
  404. static char        ttisstds[TZ_MAX_TYPES];
  405. static char        chars[TZ_MAX_CHARS];
  406. static time_t        trans[TZ_MAX_LEAPS];
  407. static long        corr[TZ_MAX_LEAPS];
  408. static char        roll[TZ_MAX_LEAPS];
  409.  
  410. /*
  411. ** Memory allocation.
  412. */
  413.  
  414. static char *
  415. memcheck(ptr)
  416. char * const    ptr;
  417. {
  418.     if (ptr == NULL) {
  419.         (void) perror(progname);
  420.         (void) exit(EXIT_FAILURE);
  421.     }
  422.     return ptr;
  423. }
  424.  
  425. #define emalloc(size)        memcheck(imalloc(size))
  426. #define erealloc(ptr, size)    memcheck(irealloc(ptr, size))
  427. #define ecpyalloc(ptr)        memcheck(icpyalloc(ptr))
  428. #define ecatalloc(oldp, newp)    memcheck(icatalloc(oldp, newp))
  429.  
  430. /*
  431. ** Error handling.
  432. */
  433.  
  434. static void
  435. eats(name, num, rname, rnum)
  436. const char * const    name;
  437. const int        num;
  438. const char * const    rname;
  439. const int        rnum;
  440. {
  441.     filename = name;
  442.     linenum = num;
  443.     rfilename = rname;
  444.     rlinenum = rnum;
  445. }
  446.  
  447. static void
  448. eat(name, num)
  449. const char * const    name;
  450. const int        num;
  451. {
  452.     eats(name, num, (char *) NULL, -1);
  453. }
  454.  
  455. static void
  456. error(string)
  457. const char * const    string;
  458. {
  459.     /*
  460.     ** Match the format of "cc" to allow sh users to
  461.     **     zic ... 2>&1 | error -t "*" -v
  462.     ** on BSD systems.
  463.     */
  464.     (void) fprintf(stderr, "\"%s\", line %d: %s",
  465.         filename, linenum, string);
  466.     if (rfilename != NULL)
  467.         (void) fprintf(stderr, " (rule from \"%s\", line %d)",
  468.             rfilename, rlinenum);
  469.     (void) fprintf(stderr, "\n");
  470.     ++errors;
  471. }
  472.  
  473. static void
  474. usage()
  475. {
  476.     (void) fprintf(stderr,
  477. "%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\
  478. \t[ -L leapseconds ] [ filename ... ]\n",
  479.         progname, progname);
  480.     (void) exit(EXIT_FAILURE);
  481. }
  482.  
  483. static const char *    psxrules = NULL;
  484. static const char *    lcltime = NULL;
  485. static const char *    directory = NULL;
  486. static const char *    leapsec = NULL;
  487. static int        sflag = FALSE;
  488.  
  489. int
  490. main(argc, argv)
  491. int    argc;
  492. char *    argv[];
  493. {
  494.     register int    i, j;
  495.     register int    c;
  496.  
  497.     (void) umask(umask(022) | 022);
  498.     progname = argv[0];
  499.     while ((c = getopt(argc, argv, "d:l:p:L:vs")) != EOF)
  500.         switch (c) {
  501.             default:
  502.                 usage();
  503.             case 'd':
  504.                 if (directory == NULL)
  505.                     directory = optarg;
  506.                 else {
  507.                     (void) fprintf(stderr,
  508. "%s: More than one -d option specified\n",
  509.                         progname);
  510.                     (void) exit(EXIT_FAILURE);
  511.                 }
  512.                 break;
  513.             case 'l':
  514.                 if (lcltime == NULL)
  515.                     lcltime = optarg;
  516.                 else {
  517.                     (void) fprintf(stderr,
  518. "%s: More than one -l option specified\n",
  519.                         progname);
  520.                     (void) exit(EXIT_FAILURE);
  521.                 }
  522.                 break;
  523.             case 'p':
  524.                 if (psxrules == NULL)
  525.                     psxrules = optarg;
  526.                 else {
  527.                     (void) fprintf(stderr,
  528. "%s: More than one -p option specified\n",
  529.                         progname);
  530.                     (void) exit(EXIT_FAILURE);
  531.                 }
  532.                 break;
  533.             case 'L':
  534.                 if (leapsec == NULL)
  535.                     leapsec = optarg;
  536.                 else {
  537.                     (void) fprintf(stderr,
  538. "%s: More than one -L option specified\n",
  539.                         progname);
  540.                     (void) exit(EXIT_FAILURE);
  541.                 }
  542.                 break;
  543.             case 'v':
  544.                 noise = TRUE;
  545.                 break;
  546.             case 's':
  547.                 sflag = TRUE;
  548.                 break;
  549.         }
  550.     if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
  551.         usage();    /* usage message by request */
  552.     if (directory == NULL)
  553.         directory = TZDIR;
  554.  
  555.     setboundaries();
  556.  
  557.     if (optind < argc && leapsec != NULL) {
  558.         infile(leapsec);
  559.         adjleap();
  560.     }
  561.  
  562.     zones = (struct zone *) emalloc(0);
  563.     rules = (struct rule *) emalloc(0);
  564.     links = (struct link *) emalloc(0);
  565.     for (i = optind; i < argc; ++i)
  566.         infile(argv[i]);
  567.     if (errors)
  568.         (void) exit(EXIT_FAILURE);
  569.     associate();
  570.     for (i = 0; i < nzones; i = j) {
  571.         /*
  572.         ** Find the next non-continuation zone entry.
  573.         */
  574.         for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
  575.             ;
  576.         outzone(&zones[i], j - i);
  577.     }
  578.     /*
  579.     ** Make links.
  580.     */
  581.     for (i = 0; i < nlinks; ++i)
  582.         dolink(links[i].l_from, links[i].l_to);
  583.     if (lcltime != NULL)
  584.         dolink(lcltime, TZDEFAULT);
  585.     if (psxrules != NULL)
  586.         dolink(psxrules, TZDEFRULES);
  587.     return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  588. }
  589.  
  590. static void
  591. dolink(fromfile, tofile)
  592. const char * const    fromfile;
  593. const char * const    tofile;
  594. {
  595.     register char *    fromname;
  596.     register char *    toname;
  597.  
  598.     fromname = ecpyalloc(directory);
  599.     fromname = ecatalloc(fromname, "/");
  600.     fromname = ecatalloc(fromname, fromfile);
  601.     toname = ecpyalloc(directory);
  602.     toname = ecatalloc(toname, "/");
  603.     toname = ecatalloc(toname, tofile);
  604.     /*
  605.     ** We get to be careful here since
  606.     ** there's a fair chance of root running us.
  607.     */
  608.     if (!itsdir(toname))
  609.         (void) remove(toname);
  610.     if (link(fromname, toname) != 0) {
  611.         (void) fprintf(stderr, "%s: Can't link from %s to ",
  612.             progname, fromname);
  613.         (void) perror(toname);
  614.         (void) exit(EXIT_FAILURE);
  615.     }
  616.     ifree(fromname);
  617.     ifree(toname);
  618. }
  619.  
  620. static void
  621. setboundaries()
  622. {
  623.     register time_t    bit;
  624.  
  625.     for (bit = 1; bit > 0; bit <<= 1)
  626.         ;
  627.     if (bit == 0) {        /* time_t is an unsigned type */
  628.         tt_signed = FALSE;
  629.         min_time = 0;
  630.         max_time = ~(time_t) 0;
  631.         if (sflag)
  632.             max_time >>= 1;
  633.     } else {
  634.         tt_signed = TRUE;
  635.         min_time = bit;
  636.         max_time = bit;
  637.         ++max_time;
  638.         max_time = -max_time;
  639.         if (sflag)
  640.             min_time = 0;
  641.     }
  642.     min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
  643.     max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
  644. }
  645.  
  646. static int
  647. itsdir(name)
  648. const char * const    name;
  649. {
  650.     struct stat    s;
  651.  
  652.     return (stat(name, &s) == 0 && S_ISDIR(s.st_mode));
  653. }
  654.  
  655. /*
  656. ** Associate sets of rules with zones.
  657. */
  658.  
  659. /*
  660. ** Sort by rule name.
  661. */
  662.  
  663. static int
  664. rcomp(cp1, cp2)
  665. const void *    cp1;
  666. const void *    cp2;
  667. {
  668.     return strcmp(((struct rule *) cp1)->r_name,
  669.         ((struct rule *) cp2)->r_name);
  670. }
  671.  
  672. static void
  673. associate()
  674. {
  675.     register struct zone *    zp;
  676.     register struct rule *    rp;
  677.     register int        base, out;
  678.     register int        i;
  679.  
  680.     if (nrules != 0)
  681.         (void) qsort((void *) rules, (size_t) nrules,
  682.              (size_t) sizeof *rules, rcomp);
  683.     for (i = 0; i < nzones; ++i) {
  684.         zp = &zones[i];
  685.         zp->z_rules = NULL;
  686.         zp->z_nrules = 0;
  687.     }
  688.     for (base = 0; base < nrules; base = out) {
  689.         rp = &rules[base];
  690.         for (out = base + 1; out < nrules; ++out)
  691.             if (strcmp(rp->r_name, rules[out].r_name) != 0)
  692.                 break;
  693.         for (i = 0; i < nzones; ++i) {
  694.             zp = &zones[i];
  695.             if (strcmp(zp->z_rule, rp->r_name) != 0)
  696.                 continue;
  697.             zp->z_rules = rp;
  698.             zp->z_nrules = out - base;
  699.         }
  700.     }
  701.     for (i = 0; i < nzones; ++i) {
  702.         zp = &zones[i];
  703.         if (zp->z_nrules == 0) {
  704.             /*
  705.             ** Maybe we have a local standard time offset.
  706.             */
  707.             eat(zp->z_filename, zp->z_linenum);
  708.             zp->z_stdoff =
  709.                 gethms((char *)zp->z_rule, "unruly zone", TRUE);
  710.             /*
  711.             ** Note, though, that if there's no rule,
  712.             ** a '%s' in the format is a bad thing.
  713.             */
  714.             if (strchr(zp->z_format, '%') != 0)
  715.                 error("%s in ruleless zone");
  716.         }
  717.     }
  718.     if (errors)
  719.         (void) exit(EXIT_FAILURE);
  720. }
  721.  
  722. static void
  723. infile(name)
  724. const char *    name;
  725. {
  726.     register FILE *            fp;
  727.     register char **        fields;
  728.     register char *            cp;
  729.     register const struct lookup *    lp;
  730.     register int            nfields;
  731.     register int            wantcont;
  732.     register int            num;
  733.     char                buf[BUFSIZ];
  734.  
  735.     if (strcmp(name, "-") == 0) {
  736.         name = "standard input";
  737.         fp = stdin;
  738.     } else if ((fp = fopen(name, "r")) == NULL) {
  739.         (void) fprintf(stderr, "%s: Can't open ", progname);
  740.         (void) perror(name);
  741.         (void) exit(EXIT_FAILURE);
  742.     }
  743.     wantcont = FALSE;
  744.     for (num = 1; ; ++num) {
  745.         eat(name, num);
  746.         if (fgets(buf, (int) sizeof buf, fp) != buf)
  747.             break;
  748.         cp = strchr(buf, '\n');
  749.         if (cp == NULL) {
  750.             error("line too long");
  751.             (void) exit(EXIT_FAILURE);
  752.         }
  753.         *cp = '\0';
  754.         fields = getfields(buf);
  755.         nfields = 0;
  756.         while (fields[nfields] != NULL) {
  757.             if (ciequal(fields[nfields], "-"))
  758.                 fields[nfields] = "";
  759.             ++nfields;
  760.         }
  761.         if (nfields == 0) {
  762.             /* nothing to do */
  763.         } else if (wantcont) {
  764.             wantcont = inzcont(fields, nfields);
  765.         } else {
  766.             lp = byword(fields[0], line_codes);
  767.             if (lp == NULL)
  768.                 error("input line of unknown type");
  769.             else switch ((int) (lp->l_value)) {
  770.                 case LC_RULE:
  771.                     inrule(fields, nfields);
  772.                     wantcont = FALSE;
  773.                     break;
  774.                 case LC_ZONE:
  775.                     wantcont = inzone(fields, nfields);
  776.                     break;
  777.                 case LC_LINK:
  778.                     inlink(fields, nfields);
  779.                     wantcont = FALSE;
  780.                     break;
  781.                 case LC_LEAP:
  782.                     if (name != leapsec)
  783.                         (void) fprintf(stderr,
  784. "%s: Leap line in non leap seconds file %s\n",
  785.                             progname, name);
  786.                     else    inleap(fields, nfields);
  787.                     wantcont = FALSE;
  788.                     break;
  789.                 default:    /* "cannot happen" */
  790.                     (void) fprintf(stderr,
  791. "%s: panic: Invalid l_value %d\n",
  792.                         progname, lp->l_value);
  793.                     (void) exit(EXIT_FAILURE);
  794.             }
  795.         }
  796.         ifree((char *) fields);
  797.     }
  798.     if (ferror(fp)) {
  799.         (void) fprintf(stderr, "%s: Error reading ", progname);
  800.         (void) perror(filename);
  801.         (void) exit(EXIT_FAILURE);
  802.     }
  803.     if (fp != stdin && fclose(fp)) {
  804.         (void) fprintf(stderr, "%s: Error closing ", progname);
  805.         (void) perror(filename);
  806.         (void) exit(EXIT_FAILURE);
  807.     }
  808.     if (wantcont)
  809.         error("expected continuation line not found");
  810. }
  811.  
  812. /*
  813. ** Convert a string of one of the forms
  814. **    h    -h     hh:mm    -hh:mm    hh:mm:ss    -hh:mm:ss
  815. ** into a number of seconds.
  816. ** A null string maps to zero.
  817. ** Call error with errstring and return zero on errors.
  818. */
  819.  
  820. static long
  821. gethms(string, errstring, signable)
  822. char *        string;
  823. const char * const    errstring;
  824. const int        signable;
  825. {
  826.     int    hh, mm, ss, sign;
  827.  
  828.     if (string == NULL || *string == '\0')
  829.         return 0;
  830.     if (!signable)
  831.         sign = 1;
  832.     else if (*string == '-') {
  833.         sign = -1;
  834.         ++string;
  835.     } else    sign = 1;
  836.     if (sscanf(string, scheck(string, "%d"), &hh) == 1)
  837.         mm = ss = 0;
  838.     else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
  839.         ss = 0;
  840.     else if (sscanf(string, scheck(string, "%d:%d:%d"),
  841.         &hh, &mm, &ss) != 3) {
  842.             error(errstring);
  843.             return 0;
  844.     }
  845.     if (hh < 0 || hh >= HOURSPERDAY ||
  846.         mm < 0 || mm >= MINSPERHOUR ||
  847.         ss < 0 || ss > SECSPERMIN) {
  848.             error(errstring);
  849.             return 0;
  850.     }
  851.     return eitol(sign) *
  852.         (eitol(hh * MINSPERHOUR + mm) *
  853.         eitol(SECSPERMIN) + eitol(ss));
  854. }
  855.  
  856. static void
  857. inrule(fields, nfields)
  858. register char ** const    fields;
  859. const int        nfields;
  860. {
  861.     static struct rule    r;
  862.  
  863.     if (nfields != RULE_FIELDS) {
  864.         error("wrong number of fields on Rule line");
  865.         return;
  866.     }
  867.     if (*fields[RF_NAME] == '\0') {
  868.         error("nameless rule");
  869.         return;
  870.     }
  871.     r.r_filename = filename;
  872.     r.r_linenum = linenum;
  873.     r.r_stdoff = gethms(fields[RF_STDOFF], "invalid saved time", TRUE);
  874.     rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
  875.         fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
  876.     r.r_name = ecpyalloc(fields[RF_NAME]);
  877.     r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
  878.     rules = (struct rule *) erealloc((char *) rules,
  879.         (int) ((nrules + 1) * sizeof *rules));
  880.     rules[nrules++] = r;
  881. }
  882.  
  883. static int
  884. inzone(fields, nfields)
  885. register char ** const    fields;
  886. const int        nfields;
  887. {
  888.     register int    i;
  889.     char        buf[132];
  890.  
  891.     if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
  892.         error("wrong number of fields on Zone line");
  893.         return FALSE;
  894.     }
  895.     if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
  896.         (void) sprintf(buf,
  897.             "\"Zone %s\" line and -l option are mutually exclusive",
  898.             TZDEFAULT);
  899.         error(buf);
  900.         return FALSE;
  901.     }
  902.     if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
  903.         (void) sprintf(buf,
  904.             "\"Zone %s\" line and -p option are mutually exclusive",
  905.             TZDEFRULES);
  906.         error(buf);
  907.         return FALSE;
  908.     }
  909.     for (i = 0; i < nzones; ++i)
  910.         if (zones[i].z_name != NULL &&
  911.             strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
  912.                 (void) sprintf(buf,
  913. "duplicate zone name %s (file \"%s\", line %d)",
  914.                     fields[ZF_NAME],
  915.                     zones[i].z_filename,
  916.                     zones[i].z_linenum);
  917.                 error(buf);
  918.                 return FALSE;
  919.         }
  920.     return inzsub(fields, nfields, FALSE);
  921. }
  922.  
  923. static int
  924. inzcont(fields, nfields)
  925. register char ** const    fields;
  926. const int        nfields;
  927. {
  928.     if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
  929.         error("wrong number of fields on Zone continuation line");
  930.         return FALSE;
  931.     }
  932.     return inzsub(fields, nfields, TRUE);
  933. }
  934.  
  935. static int
  936. inzsub(fields, nfields, iscont)
  937. register char ** const    fields;
  938. const int        nfields;
  939. const int        iscont;
  940. {
  941.     register char *        cp;
  942.     static struct zone    z;
  943.     register int        i_gmtoff, i_rule, i_format;
  944.     register int        i_untilyear, i_untilmonth;
  945.     register int        i_untilday, i_untiltime;
  946.     register int        hasuntil;
  947.  
  948.     if (iscont) {
  949.         i_gmtoff = ZFC_GMTOFF;
  950.         i_rule = ZFC_RULE;
  951.         i_format = ZFC_FORMAT;
  952.         i_untilyear = ZFC_TILYEAR;
  953.         i_untilmonth = ZFC_TILMONTH;
  954.         i_untilday = ZFC_TILDAY;
  955.         i_untiltime = ZFC_TILTIME;
  956.         z.z_name = NULL;
  957.     } else {
  958.         i_gmtoff = ZF_GMTOFF;
  959.         i_rule = ZF_RULE;
  960.         i_format = ZF_FORMAT;
  961.         i_untilyear = ZF_TILYEAR;
  962.         i_untilmonth = ZF_TILMONTH;
  963.         i_untilday = ZF_TILDAY;
  964.         i_untiltime = ZF_TILTIME;
  965.         z.z_name = ecpyalloc(fields[ZF_NAME]);
  966.     }
  967.     z.z_filename = filename;
  968.     z.z_linenum = linenum;
  969.     z.z_gmtoff = gethms(fields[i_gmtoff], "invalid GMT offset", TRUE);
  970.     if ((cp = strchr(fields[i_format], '%')) != 0) {
  971.         if (*++cp != 's' || strchr(cp, '%') != 0) {
  972.             error("invalid abbreviation format");
  973.             return FALSE;
  974.         }
  975.     }
  976.     z.z_rule = ecpyalloc(fields[i_rule]);
  977.     z.z_format = ecpyalloc(fields[i_format]);
  978.     hasuntil = nfields > i_untilyear;
  979.     if (hasuntil) {
  980.         z.z_untilrule.r_filename = filename;
  981.         z.z_untilrule.r_linenum = linenum;
  982.         rulesub(&z.z_untilrule,
  983.             fields[i_untilyear],
  984.             "only",
  985.             "",
  986.             (nfields > i_untilmonth) ? fields[i_untilmonth] : "Jan",
  987.             (nfields > i_untilday) ? fields[i_untilday] : "1",
  988.             (nfields > i_untiltime) ? fields[i_untiltime] : "0");
  989.         z.z_untiltime = rpytime(&z.z_untilrule, z.z_untilrule.r_loyear);
  990.         if (iscont && nzones > 0 && z.z_untiltime < max_time &&
  991.             z.z_untiltime > min_time &&
  992.             zones[nzones - 1].z_untiltime >= z.z_untiltime) {
  993. error("Zone continuation line end time is not after end time of previous line");
  994.             return FALSE;
  995.         }
  996.     }
  997.     zones = (struct zone *) erealloc((char *) zones,
  998.         (int) ((nzones + 1) * sizeof *zones));
  999.     zones[nzones++] = z;
  1000.     /*
  1001.     ** If there was an UNTIL field on this line,
  1002.     ** there's more information about the zone on the next line.
  1003.     */
  1004.     return hasuntil;
  1005. }
  1006.  
  1007. static void
  1008. inleap(fields, nfields)
  1009. register char ** const    fields;
  1010. const int        nfields;
  1011. {
  1012.     register const char *        cp;
  1013.     register const struct lookup *    lp;
  1014.     register int            i, j;
  1015.     int                year, month, day;
  1016.     long                dayoff, tod;
  1017.     time_t                t;
  1018.  
  1019.     if (nfields != LEAP_FIELDS) {
  1020.         error("wrong number of fields on Leap line");
  1021.         return;
  1022.     }
  1023.     dayoff = 0;
  1024.     cp = fields[LP_YEAR];
  1025.     if (sscanf((char *)cp, scheck(cp, "%d"), &year) != 1 ||
  1026.         year < min_year || year > max_year) {
  1027.             /*
  1028.              * Leapin' Lizards!
  1029.              */
  1030.             error("invalid leaping year");
  1031.             return;
  1032.     }
  1033.     j = EPOCH_YEAR;
  1034.     while (j != year) {
  1035.         if (year > j) {
  1036.             i = len_years[isleap(j)];
  1037.             ++j;
  1038.         } else {
  1039.             --j;
  1040.             i = -len_years[isleap(j)];
  1041.         }
  1042.         dayoff = oadd(dayoff, eitol(i));
  1043.     }
  1044.     if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
  1045.         error("invalid month name");
  1046.         return;
  1047.     }
  1048.     month = lp->l_value;
  1049.     j = TM_JANUARY;
  1050.     while (j != month) {
  1051.         i = len_months[isleap(year)][j];
  1052.         dayoff = oadd(dayoff, eitol(i));
  1053.         ++j;
  1054.     }
  1055.     cp = fields[LP_DAY];
  1056.     if (sscanf((char *)cp, scheck(cp, "%d"), &day) != 1 ||
  1057.         day <= 0 || day > len_months[isleap(year)][month]) {
  1058.             error("invalid day of month");
  1059.             return;
  1060.     }
  1061.     dayoff = oadd(dayoff, eitol(day - 1));
  1062.     if (dayoff < 0 && !tt_signed) {
  1063.         error("time before zero");
  1064.         return;
  1065.     }
  1066.     t = (time_t) dayoff * SECSPERDAY;
  1067.     /*
  1068.     ** Cheap overflow check.
  1069.     */
  1070.     if (t / SECSPERDAY != dayoff) {
  1071.         error("time overflow");
  1072.         return;
  1073.     }
  1074.     tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
  1075.     cp = fields[LP_CORR];
  1076.     if (strcmp(cp, "+") != 0 && strcmp(cp, "") != 0) {
  1077.         /* infile() turned "-" into "" */
  1078.         error("illegal CORRECTION field on Leap line");
  1079.         return;
  1080.     }
  1081.     if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
  1082.         error("illegal Rolling/Stationary field on Leap line");
  1083.         return;
  1084.     }
  1085.     addleap(tadd(t, tod), *cp == '+', lp->l_value);
  1086. }
  1087.  
  1088. static void
  1089. inlink(fields, nfields)
  1090. register char ** const    fields;
  1091. const int        nfields;
  1092. {
  1093.     struct link    l;
  1094.  
  1095.     if (nfields != LINK_FIELDS) {
  1096.         error("wrong number of fields on Link line");
  1097.         return;
  1098.     }
  1099.     if (*fields[LF_FROM] == '\0') {
  1100.         error("blank FROM field on Link line");
  1101.         return;
  1102.     }
  1103.     if (*fields[LF_TO] == '\0') {
  1104.         error("blank TO field on Link line");
  1105.         return;
  1106.     }
  1107.     l.l_filename = filename;
  1108.     l.l_linenum = linenum;
  1109.     l.l_from = ecpyalloc(fields[LF_FROM]);
  1110.     l.l_to = ecpyalloc(fields[LF_TO]);
  1111.     links = (struct link *) erealloc((char *) links,
  1112.         (int) ((nlinks + 1) * sizeof *links));
  1113.     links[nlinks++] = l;
  1114. }
  1115.  
  1116. static void
  1117. rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
  1118. register struct rule * const    rp;
  1119. char * const            loyearp;
  1120. char * const            hiyearp;
  1121. char * const            typep;
  1122. char * const            monthp;
  1123. char * const            dayp;
  1124. char * const            timep;
  1125. {
  1126.     register struct lookup const *    lp;
  1127.     register char *            cp;
  1128.  
  1129.     if ((lp = byword(monthp, mon_names)) == NULL) {
  1130.         error("invalid month name");
  1131.         return;
  1132.     }
  1133.     rp->r_month = lp->l_value;
  1134.     rp->r_todisstd = FALSE;
  1135.     cp = timep;
  1136.     if (*cp != '\0') {
  1137.         cp += strlen(cp) - 1;
  1138.         switch (lowerit(*cp)) {
  1139.             case 's':
  1140.                 rp->r_todisstd = TRUE;
  1141.                 *cp = '\0';
  1142.                 break;
  1143.             case 'w':
  1144.                 rp->r_todisstd = FALSE;
  1145.                 *cp = '\0';
  1146.                 break;
  1147.         }
  1148.     }
  1149.     rp->r_tod = gethms(timep, "invalid time of day", FALSE);
  1150.     /*
  1151.     ** Year work.
  1152.     */
  1153.     cp = loyearp;
  1154.     if ((lp = byword(cp, begin_years)) != NULL) switch ((int) lp->l_value) {
  1155.         case YR_MINIMUM:
  1156.             rp->r_loyear = min_year;
  1157.             break;
  1158.         case YR_MAXIMUM:
  1159.             rp->r_loyear = max_year;
  1160.             break;
  1161.         default:    /* "cannot happen" */
  1162.             (void) fprintf(stderr,
  1163.                 "%s: panic: Invalid l_value %d\n",
  1164.                 progname, lp->l_value);
  1165.             (void) exit(EXIT_FAILURE);
  1166.     } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1 ||
  1167.         rp->r_loyear < min_year || rp->r_loyear > max_year) {
  1168.             if (noise)
  1169.                 error("invalid starting year");
  1170.             if (rp->r_loyear > max_year)
  1171.                 return;
  1172.     }
  1173.     cp = hiyearp;
  1174.     if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
  1175.         case YR_MINIMUM:
  1176.             rp->r_hiyear = min_year;
  1177.             break;
  1178.         case YR_MAXIMUM:
  1179.             rp->r_hiyear = max_year;
  1180.             break;
  1181.         case YR_ONLY:
  1182.             rp->r_hiyear = rp->r_loyear;
  1183.             break;
  1184.         default:    /* "cannot happen" */
  1185.             (void) fprintf(stderr,
  1186.                 "%s: panic: Invalid l_value %d\n",
  1187.                 progname, lp->l_value);
  1188.             (void) exit(EXIT_FAILURE);
  1189.     } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1 ||
  1190.         rp->r_hiyear < min_year || rp->r_hiyear > max_year) {
  1191.             if (noise)
  1192.                 error("invalid ending year");
  1193.             if (rp->r_hiyear < min_year)
  1194.                 return;
  1195.     }
  1196.     if (rp->r_hiyear < min_year)
  1197.          return;
  1198.      if (rp->r_loyear < min_year)
  1199.          rp->r_loyear = min_year;
  1200.      if (rp->r_hiyear > max_year)
  1201.          rp->r_hiyear = max_year;
  1202.     if (rp->r_loyear > rp->r_hiyear) {
  1203.         error("starting year greater than ending year");
  1204.         return;
  1205.     }
  1206.     if (*typep == '\0')
  1207.         rp->r_yrtype = NULL;
  1208.     else {
  1209.         if (rp->r_loyear == rp->r_hiyear) {
  1210.             error("typed single year");
  1211.             return;
  1212.         }
  1213.         rp->r_yrtype = ecpyalloc(typep);
  1214.     }
  1215.     /*
  1216.     ** Day work.
  1217.     ** Accept things such as:
  1218.     **    1
  1219.     **    last-Sunday
  1220.     **    Sun<=20
  1221.     **    Sun>=7
  1222.     */
  1223.     if ((lp = byword(dayp, lasts)) != NULL) {
  1224.         rp->r_dycode = DC_DOWLEQ;
  1225.         rp->r_wday = lp->l_value;
  1226.         rp->r_dayofmonth = len_months[1][rp->r_month];
  1227.     } else {
  1228.         if ((cp = strchr(dayp, '<')) != 0)
  1229.             rp->r_dycode = DC_DOWLEQ;
  1230.         else if ((cp = strchr(dayp, '>')) != 0)
  1231.             rp->r_dycode = DC_DOWGEQ;
  1232.         else {
  1233.             cp = dayp;
  1234.             rp->r_dycode = DC_DOM;
  1235.         }
  1236.         if (rp->r_dycode != DC_DOM) {
  1237.             *cp++ = 0;
  1238.             if (*cp++ != '=') {
  1239.                 error("invalid day of month");
  1240.                 return;
  1241.             }
  1242.             if ((lp = byword(dayp, wday_names)) == NULL) {
  1243.                 error("invalid weekday name");
  1244.                 return;
  1245.             }
  1246.             rp->r_wday = lp->l_value;
  1247.         }
  1248.         if (sscanf(cp, scheck(cp, "%d"), &rp->r_dayofmonth) != 1 ||
  1249.             rp->r_dayofmonth <= 0 ||
  1250.             (rp->r_dayofmonth > len_months[1][rp->r_month])) {
  1251.                 error("invalid day of month");
  1252.                 return;
  1253.         }
  1254.     }
  1255. }
  1256.  
  1257. static void
  1258. convert(val, buf)
  1259. const long    val;
  1260. char * const    buf;
  1261. {
  1262.     register int    i;
  1263.     register long    shift;
  1264.  
  1265.     for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
  1266.         buf[i] = val >> shift;
  1267. }
  1268.  
  1269. static void
  1270. puttzcode(val, fp)
  1271. const long    val;
  1272. FILE * const    fp;
  1273. {
  1274.     char    buf[4];
  1275.  
  1276.     convert(val, buf);
  1277.     (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
  1278. }
  1279.  
  1280. static void
  1281. writezone(name)
  1282. const char * const    name;
  1283. {
  1284.     register FILE *        fp;
  1285.     register int        i, j;
  1286.     char            fullname[BUFSIZ];
  1287.     static struct tzhead    tzh;
  1288.  
  1289.     if (strlen(directory) + 1 + strlen(name) >= sizeof fullname) {
  1290.         (void) fprintf(stderr,
  1291.             "%s: File name %s/%s too long\n", progname,
  1292.             directory, name);
  1293.         (void) exit(EXIT_FAILURE);
  1294.     }
  1295.     (void) sprintf(fullname, "%s/%s", directory, name);
  1296.     if ((fp = fopen(fullname, "wb")) == NULL) {
  1297.         if (mkdirs(fullname) != 0)
  1298.             (void) exit(EXIT_FAILURE);
  1299.         if ((fp = fopen(fullname, "wb")) == NULL) {
  1300.             (void) fprintf(stderr, "%s: Can't create ", progname);
  1301.             (void) perror(fullname);
  1302.             (void) exit(EXIT_FAILURE);
  1303.         }
  1304.     }
  1305.     convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
  1306.     convert(eitol(leapcnt), tzh.tzh_leapcnt);
  1307.     convert(eitol(timecnt), tzh.tzh_timecnt);
  1308.     convert(eitol(typecnt), tzh.tzh_typecnt);
  1309.     convert(eitol(charcnt), tzh.tzh_charcnt);
  1310.     (void) fwrite((void *) &tzh, (size_t) sizeof tzh, (size_t) 1, fp);
  1311.     for (i = 0; i < timecnt; ++i) {
  1312.         j = leapcnt;
  1313.         while (--j >= 0)
  1314.             if (ats[i] >= trans[j]) {
  1315.                 ats[i] = tadd(ats[i], corr[j]);
  1316.                 break;
  1317.             }
  1318.         puttzcode((long) ats[i], fp);
  1319.     }
  1320.     if (timecnt > 0)
  1321.         (void) fwrite((void *) types, (size_t) sizeof types[0],
  1322.             (size_t) timecnt, fp);
  1323.     for (i = 0; i < typecnt; ++i) {
  1324.         puttzcode((long) gmtoffs[i], fp);
  1325.         (void) putc(isdsts[i], fp);
  1326.         (void) putc(abbrinds[i], fp);
  1327.     }
  1328.     if (charcnt != 0)
  1329.         (void) fwrite((void *) chars, (size_t) sizeof chars[0],
  1330.             (size_t) charcnt, fp);
  1331.     for (i = 0; i < leapcnt; ++i) {
  1332.         if (roll[i]) {
  1333.             if (timecnt == 0 || trans[i] < ats[0]) {
  1334.                 j = 0;
  1335.                 while (isdsts[j])
  1336.                     if (++j >= typecnt) {
  1337.                         j = 0;
  1338.                         break;
  1339.                     }
  1340.             } else {
  1341.                 j = 1;
  1342.                 while (j < timecnt && trans[i] >= ats[j])
  1343.                     ++j;
  1344.                 j = types[j - 1];
  1345.             }
  1346.             puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
  1347.         } else    puttzcode((long) trans[i], fp);
  1348.         puttzcode((long) corr[i], fp);
  1349.     }
  1350.     for (i = 0; i < typecnt; ++i)
  1351.         (void) putc(ttisstds[i], fp);
  1352.     if (ferror(fp) || fclose(fp)) {
  1353.         (void) fprintf(stderr, "%s: Write error on ", progname);
  1354.         (void) perror(fullname);
  1355.         (void) exit(EXIT_FAILURE);
  1356.     }
  1357. }
  1358.  
  1359. static void
  1360. outzone(zpfirst, zonecount)
  1361. const struct zone * const    zpfirst;
  1362. const int            zonecount;
  1363. {
  1364.     register const struct zone *    zp;
  1365.     register struct rule *        rp;
  1366.     register int            i, j;
  1367.     register int            usestart, useuntil;
  1368.     register time_t            starttime, untiltime;
  1369.     register long            gmtoff;
  1370.     register long            stdoff;
  1371.     register int            year;
  1372.     register long            startoff;
  1373.     register int            startisdst;
  1374.     register int            startttisstd;
  1375.     register int            type;
  1376.     char                startbuf[BUFSIZ];
  1377.  
  1378.     /*
  1379.     ** Now. . .finally. . .generate some useful data!
  1380.     */
  1381.     timecnt = 0;
  1382.     typecnt = 0;
  1383.     charcnt = 0;
  1384.     /*
  1385.     ** Two guesses. . .the second may well be corrected later.
  1386.     */
  1387.     gmtoff = zpfirst->z_gmtoff;
  1388.     stdoff = 0;
  1389. #ifdef lint
  1390.     starttime = 0;
  1391.     startttisstd = FALSE;
  1392. #endif /* defined lint */
  1393.     for (i = 0; i < zonecount; ++i) {
  1394.         usestart = i > 0;
  1395.         useuntil = i < (zonecount - 1);
  1396.         zp = &zpfirst[i];
  1397.         eat(zp->z_filename, zp->z_linenum);
  1398.         startisdst = -1;
  1399.         if (zp->z_nrules == 0) {
  1400.             type = addtype(oadd(zp->z_gmtoff, zp->z_stdoff),
  1401.                 zp->z_format, zp->z_stdoff != 0,
  1402.                 startttisstd);
  1403.             if (usestart)
  1404.                 addtt(starttime, type);
  1405.             gmtoff = zp->z_gmtoff;
  1406.             stdoff = zp->z_stdoff;
  1407.         } else for (year = min_year; year <= max_year; ++year) {
  1408.             if (useuntil && year > zp->z_untilrule.r_hiyear)
  1409.                 break;
  1410.             /*
  1411.             ** Mark which rules to do in the current year.
  1412.             ** For those to do, calculate rpytime(rp, year);
  1413.             */
  1414.             for (j = 0; j < zp->z_nrules; ++j) {
  1415.                 rp = &zp->z_rules[j];
  1416.                 eats(zp->z_filename, zp->z_linenum,
  1417.                     rp->r_filename, rp->r_linenum);
  1418.                 rp->r_todo = year >= rp->r_loyear &&
  1419.                         year <= rp->r_hiyear &&
  1420.                         yearistype(year, rp->r_yrtype);
  1421.                 if (rp->r_todo)
  1422.                     rp->r_temp = rpytime(rp, year);
  1423.             }
  1424.             for ( ; ; ) {
  1425.                 register int    k;
  1426.                 register time_t    jtime, ktime;
  1427.                 register long    offset;
  1428.                 char        buf[BUFSIZ];
  1429.  
  1430.                 if (useuntil) {
  1431.                     /*
  1432.                     ** Turn untiltime into GMT
  1433.                     ** assuming the current gmtoff and
  1434.                     ** stdoff values.
  1435.                     */
  1436.                     offset = gmtoff;
  1437.                     if (!zp->z_untilrule.r_todisstd)
  1438.                         offset = oadd(offset, stdoff);
  1439.                     untiltime = tadd(zp->z_untiltime,
  1440.                         -offset);
  1441.                 }
  1442.                 /*
  1443.                 ** Find the rule (of those to do, if any)
  1444.                 ** that takes effect earliest in the year.
  1445.                 */
  1446.                 k = -1;
  1447. #ifdef lint
  1448.                 ktime = 0;
  1449. #endif /* defined lint */
  1450.                 for (j = 0; j < zp->z_nrules; ++j) {
  1451.                     rp = &zp->z_rules[j];
  1452.                     if (!rp->r_todo)
  1453.                         continue;
  1454.                     eats(zp->z_filename, zp->z_linenum,
  1455.                         rp->r_filename, rp->r_linenum);
  1456.                     offset = gmtoff;
  1457.                     if (!rp->r_todisstd)
  1458.                         offset = oadd(offset, stdoff);
  1459.                     jtime = rp->r_temp;
  1460.                     if (jtime == min_time ||
  1461.                         jtime == max_time)
  1462.                             continue;
  1463.                     jtime = tadd(jtime, -offset);
  1464.                     if (k < 0 || jtime < ktime) {
  1465.                         k = j;
  1466.                         ktime = jtime;
  1467.                     }
  1468.                 }
  1469.                 if (k < 0)
  1470.                     break;    /* go on to next year */
  1471.                 rp = &zp->z_rules[k];
  1472.                 rp->r_todo = FALSE;
  1473.                 if (useuntil && ktime >= untiltime)
  1474.                     break;
  1475.                 if (usestart) {
  1476.                     if (ktime < starttime) {
  1477.                         stdoff = rp->r_stdoff;
  1478.                         startoff = oadd(zp->z_gmtoff,
  1479.                             rp->r_stdoff);
  1480.                         (void) sprintf(startbuf,
  1481.                             zp->z_format,
  1482.                             rp->r_abbrvar);
  1483.                         startisdst =
  1484.                             rp->r_stdoff != 0;
  1485.                         continue;
  1486.                     }
  1487.                     if (ktime != starttime &&
  1488.                         startisdst >= 0)
  1489. addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd));
  1490.                     usestart = FALSE;
  1491.                 }
  1492.                 eats(zp->z_filename, zp->z_linenum,
  1493.                     rp->r_filename, rp->r_linenum);
  1494.                 (void) sprintf(buf, zp->z_format,
  1495.                     rp->r_abbrvar);
  1496.                 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
  1497.                 type = addtype(offset, buf, rp->r_stdoff != 0,
  1498.                     rp->r_todisstd);
  1499.                 if (timecnt != 0 || rp->r_stdoff != 0)
  1500.                     addtt(ktime, type);
  1501.                 gmtoff = zp->z_gmtoff;
  1502.                 stdoff = rp->r_stdoff;
  1503.             }
  1504.         }
  1505.         /*
  1506.         ** Now we may get to set starttime for the next zone line.
  1507.         */
  1508.         if (useuntil) {
  1509.             starttime = tadd(zp->z_untiltime,
  1510.                 -gmtoffs[types[timecnt - 1]]);
  1511.             startttisstd = zp->z_untilrule.r_todisstd;
  1512.         }
  1513.     }
  1514.     writezone(zpfirst->z_name);
  1515. }
  1516.  
  1517. static void
  1518. addtt(starttime, type)
  1519. const time_t    starttime;
  1520. const int    type;
  1521. {
  1522.     if (timecnt != 0 && type == types[timecnt - 1])
  1523.         return;    /* easy enough! */
  1524.     if (timecnt >= TZ_MAX_TIMES) {
  1525.         error("too many transitions?!");
  1526.         (void) exit(EXIT_FAILURE);
  1527.     }
  1528.     ats[timecnt] = starttime;
  1529.     types[timecnt] = type;
  1530.     ++timecnt;
  1531. }
  1532.  
  1533. static int
  1534. addtype(gmtoff, abbr, isdst, ttisstd)
  1535. const long        gmtoff;
  1536. const char * const    abbr;
  1537. const int        isdst;
  1538. const int        ttisstd;
  1539. {
  1540.     register int    i, j;
  1541.  
  1542.     /*
  1543.     ** See if there's already an entry for this zone type.
  1544.     ** If so, just return its index.
  1545.     */
  1546.     for (i = 0; i < typecnt; ++i) {
  1547.         if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
  1548.             strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
  1549.             ttisstd == ttisstds[i])
  1550.                 return i;
  1551.     }
  1552.     /*
  1553.     ** There isn't one; add a new one, unless there are already too
  1554.     ** many.
  1555.     */
  1556.     if (typecnt >= TZ_MAX_TYPES) {
  1557.         error("too many local time types");
  1558.         (void) exit(EXIT_FAILURE);
  1559.     }
  1560.     gmtoffs[i] = gmtoff;
  1561.     isdsts[i] = isdst;
  1562.     ttisstds[i] = ttisstd;
  1563.  
  1564.     for (j = 0; j < charcnt; ++j)
  1565.         if (strcmp(&chars[j], abbr) == 0)
  1566.             break;
  1567.     if (j == charcnt)
  1568.         newabbr(abbr);
  1569.     abbrinds[i] = j;
  1570.     ++typecnt;
  1571.     return i;
  1572. }
  1573.  
  1574. static void
  1575. addleap(t, positive, rolling)
  1576. const time_t    t;
  1577. const int    positive;
  1578. const int    rolling;
  1579. {
  1580.     register int    i, j;
  1581.  
  1582.     if (leapcnt >= TZ_MAX_LEAPS) {
  1583.         error("too many leap seconds");
  1584.         (void) exit(EXIT_FAILURE);
  1585.     }
  1586.     for (i = 0; i < leapcnt; ++i)
  1587.         if (t <= trans[i]) {
  1588.             if (t == trans[i]) {
  1589.                 error("repeated leap second moment");
  1590.                 (void) exit(EXIT_FAILURE);
  1591.             }
  1592.             break;
  1593.         }
  1594.     for (j = leapcnt; j > i; --j) {
  1595.         trans[j] = trans[j-1];
  1596.         corr[j] = corr[j-1];
  1597.         roll[j] = roll[j-1];
  1598.     }
  1599.     trans[i] = t;
  1600.     corr[i] = (positive ? 1L : -1L);
  1601.     roll[i] = rolling;
  1602.     ++leapcnt;
  1603. }
  1604.  
  1605. static void
  1606. adjleap()
  1607. {
  1608.     register int    i;
  1609.     register long    last = 0;
  1610.  
  1611.     /*
  1612.     ** propagate leap seconds forward
  1613.     */
  1614.     for (i = 0; i < leapcnt; ++i) {
  1615.         trans[i] = tadd(trans[i], last);
  1616.         last = corr[i] += last;
  1617.     }
  1618. }
  1619.  
  1620. static int
  1621. yearistype(year, type)
  1622. const int        year;
  1623. const char * const    type;
  1624. {
  1625.     char    buf[BUFSIZ];
  1626.     int    result;
  1627.  
  1628.     if (type == NULL || *type == '\0')
  1629.         return TRUE;
  1630.     if (strcmp(type, "uspres") == 0)
  1631.         return (year % 4) == 0;
  1632.     if (strcmp(type, "nonpres") == 0)
  1633.         return (year % 4) != 0;
  1634.     (void) sprintf(buf, "yearistype %d %s", year, type);
  1635.     result = system(buf);
  1636.     if (result == 0)
  1637.         return TRUE;
  1638.     if (result == (1 << 8))
  1639.         return FALSE;
  1640.     error("Wild result from command execution");
  1641.     (void) fprintf(stderr, "%s: command was '%s', result was %d\n",
  1642.         progname, buf, result);
  1643.     for ( ; ; )
  1644.         (void) exit(EXIT_FAILURE);
  1645. }
  1646.  
  1647. static int
  1648. lowerit(a)
  1649. const int    a;
  1650. {
  1651.     return (isascii(a) && isupper(a)) ? tolower(a) : a;
  1652. }
  1653.  
  1654. static int
  1655. ciequal(ap, bp)        /* case-insensitive equality */
  1656. register const char *    ap;
  1657. register const char *    bp;
  1658. {
  1659.     while (lowerit(*ap) == lowerit(*bp++))
  1660.         if (*ap++ == '\0')
  1661.             return TRUE;
  1662.     return FALSE;
  1663. }
  1664.  
  1665. static int
  1666. itsabbr(abbr, word)
  1667. register const char *    abbr;
  1668. register const char *    word;
  1669. {
  1670.     if (lowerit(*abbr) != lowerit(*word))
  1671.         return FALSE;
  1672.     ++word;
  1673.     while (*++abbr != '\0')
  1674.         do if (*word == '\0')
  1675.             return FALSE;
  1676.                 while (lowerit(*word++) != lowerit(*abbr));
  1677.     return TRUE;
  1678. }
  1679.  
  1680. static const struct lookup *
  1681. byword(word, table)
  1682. register const char * const        word;
  1683. register const struct lookup * const    table;
  1684. {
  1685.     register const struct lookup *    foundlp;
  1686.     register const struct lookup *    lp;
  1687.  
  1688.     if (word == NULL || table == NULL)
  1689.         return NULL;
  1690.     /*
  1691.     ** Look for exact match.
  1692.     */
  1693.     for (lp = table; lp->l_word != NULL; ++lp)
  1694.         if (ciequal(word, lp->l_word))
  1695.             return lp;
  1696.     /*
  1697.     ** Look for inexact match.
  1698.     */
  1699.     foundlp = NULL;
  1700.     for (lp = table; lp->l_word != NULL; ++lp)
  1701.         if (itsabbr(word, lp->l_word))
  1702.             if (foundlp == NULL)
  1703.                 foundlp = lp;
  1704.             else    return NULL;    /* multiple inexact matches */
  1705.     return foundlp;
  1706. }
  1707.  
  1708. static char **
  1709. getfields(cp)
  1710. register char *    cp;
  1711. {
  1712.     register char *        dp;
  1713.     register char **    array;
  1714.     register int        nsubs;
  1715.  
  1716.     if (cp == NULL)
  1717.         return NULL;
  1718.     array = (char **) emalloc((int) ((strlen(cp) + 1) * sizeof *array));
  1719.     nsubs = 0;
  1720.     for ( ; ; ) {
  1721.         while (isascii(*cp) && isspace(*cp))
  1722.             ++cp;
  1723.         if (*cp == '\0' || *cp == '#')
  1724.             break;
  1725.         array[nsubs++] = dp = cp;
  1726.         do {
  1727.             if ((*dp = *cp++) != '"')
  1728.                 ++dp;
  1729.             else while ((*dp = *cp++) != '"')
  1730.                 if (*dp != '\0')
  1731.                     ++dp;
  1732.                 else    error("Odd number of quotation marks");
  1733.         } while (*cp != '\0' && *cp != '#' &&
  1734.             (!isascii(*cp) || !isspace(*cp)));
  1735.         if (isascii(*cp) && isspace(*cp))
  1736.             ++cp;
  1737.         *dp = '\0';
  1738.     }
  1739.     array[nsubs] = NULL;
  1740.     return array;
  1741. }
  1742.  
  1743. static long
  1744. oadd(t1, t2)
  1745. const long    t1;
  1746. const long    t2;
  1747. {
  1748.     register long    t;
  1749.  
  1750.     t = t1 + t2;
  1751.     if (t2 > 0 && t <= t1 || t2 < 0 && t >= t1) {
  1752.         error("time overflow");
  1753.         (void) exit(EXIT_FAILURE);
  1754.     }
  1755.     return t;
  1756. }
  1757.  
  1758. static time_t
  1759. tadd(t1, t2)
  1760. const time_t    t1;
  1761. const long    t2;
  1762. {
  1763.     register time_t    t;
  1764.  
  1765.     if (t1 == max_time && t2 > 0)
  1766.         return max_time;
  1767.     if (t1 == min_time && t2 < 0)
  1768.         return min_time;
  1769.     t = t1 + t2;
  1770.     if (t2 > 0 && t <= t1 || t2 < 0 && t >= t1) {
  1771.         error("time overflow");
  1772.         (void) exit(EXIT_FAILURE);
  1773.     }
  1774.     return t;
  1775. }
  1776.  
  1777. /*
  1778. ** Given a rule, and a year, compute the date - in seconds since January 1,
  1779. ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
  1780. */
  1781.  
  1782. static time_t
  1783. rpytime(rp, wantedy)
  1784. register const struct rule * const    rp;
  1785. register const int            wantedy;
  1786. {
  1787.     register int    y, m, i;
  1788.     register long    dayoff;            /* with a nod to Margaret O. */
  1789.     register time_t    t;
  1790.  
  1791.     dayoff = 0;
  1792.     m = TM_JANUARY;
  1793.     y = EPOCH_YEAR;
  1794.     while (wantedy != y) {
  1795.         if (wantedy > y) {
  1796.             i = len_years[isleap(y)];
  1797.             ++y;
  1798.         } else {
  1799.             --y;
  1800.             i = -len_years[isleap(y)];
  1801.         }
  1802.         dayoff = oadd(dayoff, eitol(i));
  1803.     }
  1804.     while (m != rp->r_month) {
  1805.         i = len_months[isleap(y)][m];
  1806.         dayoff = oadd(dayoff, eitol(i));
  1807.         ++m;
  1808.     }
  1809.     i = rp->r_dayofmonth;
  1810.     if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
  1811.         if (rp->r_dycode == DC_DOWLEQ)
  1812.             --i;
  1813.         else {
  1814.             error("use of 2/29 in non leap-year");
  1815.             (void) exit(EXIT_FAILURE);
  1816.         }
  1817.     }
  1818.     --i;
  1819.     dayoff = oadd(dayoff, eitol(i));
  1820.     if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
  1821.         register long    wday;
  1822.  
  1823. #define LDAYSPERWEEK    ((long) DAYSPERWEEK)
  1824.         wday = eitol(EPOCH_WDAY);
  1825.         /*
  1826.         ** Don't trust mod of negative numbers.
  1827.         */
  1828.         if (dayoff >= 0)
  1829.             wday = (wday + dayoff) % LDAYSPERWEEK;
  1830.         else {
  1831.             wday -= ((-dayoff) % LDAYSPERWEEK);
  1832.             if (wday < 0)
  1833.                 wday += LDAYSPERWEEK;
  1834.         }
  1835.         while (wday != eitol(rp->r_wday))
  1836.             if (rp->r_dycode == DC_DOWGEQ) {
  1837.                 dayoff = oadd(dayoff, (long) 1);
  1838.                 if (++wday >= LDAYSPERWEEK)
  1839.                     wday = 0;
  1840.                 ++i;
  1841.             } else {
  1842.                 dayoff = oadd(dayoff, (long) -1);
  1843.                 if (--wday < 0)
  1844.                     wday = LDAYSPERWEEK;
  1845.                 --i;
  1846.             }
  1847.         if (i < 0 || i >= len_months[isleap(y)][m]) {
  1848.             error("no day in month matches rule");
  1849.             (void) exit(EXIT_FAILURE);
  1850.         }
  1851.     }
  1852.     if (dayoff < 0 && !tt_signed) {
  1853.         if (wantedy == rp->r_loyear)
  1854.             return min_time;
  1855.         error("time before zero");
  1856.         (void) exit(EXIT_FAILURE);
  1857.     }
  1858.     t = (time_t) dayoff * SECSPERDAY;
  1859.     /*
  1860.     ** Cheap overflow check.
  1861.     */
  1862.     if (t / SECSPERDAY != dayoff) {
  1863.         if (wantedy == rp->r_hiyear)
  1864.             return max_time;
  1865.         if (wantedy == rp->r_loyear)
  1866.             return min_time;
  1867.         error("time overflow");
  1868.         (void) exit(EXIT_FAILURE);
  1869.     }
  1870.     return tadd(t, rp->r_tod);
  1871. }
  1872.  
  1873. static void
  1874. newabbr(string)
  1875. const char * const    string;
  1876. {
  1877.     register int    i;
  1878.  
  1879.     i = strlen(string) + 1;
  1880.     if (charcnt + i >= TZ_MAX_CHARS) {
  1881.         error("too many, or too long, time zone abbreviations");
  1882.         (void) exit(EXIT_FAILURE);
  1883.     }
  1884.     (void) strcpy(&chars[charcnt], string);
  1885.     charcnt += eitol(i);
  1886. }
  1887.  
  1888. static int
  1889. mkdirs(name)
  1890. char * const    name;
  1891. {
  1892.     register char *    cp;
  1893.  
  1894.     if ((cp = name) == NULL || *cp == '\0')
  1895.         return 0;
  1896.     while ((cp = strchr(cp + 1, '/')) != 0) {
  1897.         *cp = '\0';
  1898.         if (!itsdir(name)) {
  1899.             /*
  1900.             ** It doesn't seem to exist, so we try to create it.
  1901.             */
  1902.             if (mkdir(name, 0755) != 0) {
  1903.                 (void) fprintf(stderr,
  1904.                     "%s: Can't create directory ",
  1905.                     progname);
  1906.                 (void) perror(name);
  1907.                 return -1;
  1908.             }
  1909.         }
  1910.         *cp = '/';
  1911.     }
  1912.     return 0;
  1913. }
  1914.  
  1915. static long
  1916. eitol(i)
  1917. const int    i;
  1918. {
  1919.     long    l;
  1920.  
  1921.     l = i;
  1922.     if (i < 0 && l >= 0 || i == 0 && l != 0 || i > 0 && l <= 0) {
  1923.         (void) fprintf(stderr, "%s: %d did not sign extend correctly\n",
  1924.             progname, i);
  1925.         (void) exit(EXIT_FAILURE);
  1926.     }
  1927.     return l;
  1928. }
  1929.  
  1930. /*
  1931. ** UNIX is a registered trademark of AT&T.
  1932. */
  1933.